package com.fitbet.wechat.handler.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.demo2do.core.utils.JsonUtils;
import com.fitbet.entity.bet.UserBet;
import com.fitbet.entity.user.Wallet;
import com.fitbet.wechat.handler.api.UserApiHandler;
import com.fitbet.wechat.rao.api.UserRao;

/**
 * 
 * @author mohanlan
 *
 */
@Component("userApiHandler")
public class UserApiHandlerImpl implements UserApiHandler {
    
    @Value("#{ctx['api.host']}")
    private String apiHost;
    
    @Rao
    private UserRao userRao;
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.handler.api.UserApiHandler#loadWallet(java.lang.Long)
     */
    public Wallet loadWallet(Long userId) {
        
        HttpResult httpResult = userRao.loadWallet(apiHost + "/walles/" + userId);
        
        if(!httpResult.isValid()) {
            throw BusinessException.newInstance(httpResult.getCode(), httpResult.getMessage());
        }
        
        Wallet wallet = JsonUtils.parse(JsonUtils.toJson(httpResult.get("wallet")), Wallet.class);
        return wallet;
    }
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.handler.api.UserApiHandler#loadUserBet(java.lang.Long, java.lang.Long)
     */
    public UserBet loadUserBet(Long userId, Long betId) {
        
        HttpResult httpResult = userRao.loadUserBet(apiHost + "/users/" + userId + "/bets/" + betId);
        if(!httpResult.isValid()) {
            throw BusinessException.newInstance(httpResult.getCode(), httpResult.getMessage());
        }
        
        UserBet userBet = JsonUtils.parse(JsonUtils.toJson(httpResult.get("userBet")), UserBet.class);
        return userBet;
    }
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.handler.api.UserApiHandler#createUserBet(java.lang.Long, java.lang.Long)
     */
    public HttpResult createUserBet(Long userId, Long betId) {
        
        HttpResult httpResult = userRao.createUserBet(apiHost + "/users/" + userId + "/bets?betId=" + betId);
        
        if(!httpResult.isValid()) {
            throw BusinessException.newInstance(httpResult.getCode(), httpResult.getMessage());
        }
        
        return httpResult;
    }
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.handler.api.UserApiHandler#listBets(java.lang.Long)
     */
    public List<Object> listBets(Long userId) {
        
        HttpResult httpResult = userRao.loadWallet(apiHost + "/users/" + userId + "/bets");
        
        if(!httpResult.isValid()) {
            throw BusinessException.newInstance(httpResult.getCode(), httpResult.getMessage());
        }
        
        // TODO get result by httpResult
        
        
        List<Object> result = new ArrayList<Object>();
        
        return result;
    }
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.handler.api.UserApiHandler#listSupports(java.lang.Long)
     */
    public List<Object> listSupports(Long userId) {
        
        HttpResult httpResult = userRao.loadWallet(apiHost + "/users/" + userId + "/supports");
        
        if(!httpResult.isValid()) {
            throw BusinessException.newInstance(httpResult.getCode(), httpResult.getMessage());
        }
        
        // TODO get result by httpResult
        List<Object> result = new ArrayList<Object>();
        
        return result;
    }

}
